import axios from 'axios'

const service = axios.create({
  baseUrl: process.env.BASE_URL,
  timeout: 20 * 1000
})

service.interceptors.request.use((config) => {
  config.headers['token'] = '123456'
  return config
})

service.interceptors.response.use(
  (res) => {
    const data = res.data
    const { code, msg } = data
    if (code !== 200) {
      // 显示报错信息 msg
      return Promise.reject(data) //如果不修改 405的时候就不会调用验证码的接口
    } else {
      return data
    }
  },
  (err) => {
    // 显示报错信息 error.message
    console.log('响应请求拦截器报错：' + err)
    ELEMENT.Message({
      message: err.msg || err.message || '请求出错',
      type: 'error',
      duration: 1000 * 2
    })
    return Promise.reject(err)
  }
)

export const postAxios = (url, data, config) => {
  return service.post(url, data, config)
}
export const getAxios = (url, params, config = {}) => {
  config.params = params
  return service.get(url, config)
}

export default service